<?php

namespace app\index\controller;

use app\common\controller\Frontend;

class Baiying extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function baiyingphonecallback()
    {        
        $flag = false;
        $request_data = $this->request->param();
//        qqq('callback',$request_data);
        
        if($request_data['code'] == 200 ){
            $data = $request_data['data'];
            switch ($data['callbackType']){
                case 'JOB_INFO_RESULT':
                    $flag = $this->_deal_calljob($data);
                    break;
                case 'CALL_INSTANCE_RESULT'://电话
                    $flag = $this->_deal_phone_call($data);
                    break;
            }
        }
        exit('success');
        
    }
    
    
    public function _deal_phone_call($param){
        $data = $param['data'];
        
        $callInstance = $data['callInstance'];
        
        if(empty($callInstance['callInstanceId'])) return false;
        
        $taskResult = $data['taskResult'];
        $yixiang_value = '' ;
        $is_yixiang = 0;
        if(!empty($taskResult)){
            foreach ($taskResult as $value){
                if($value['resultName'] == '客户意向等级'){
                    $yixiang_value = $value['resultValue'];
                    break;
                }
            }
        }

        if(!empty($yixiang_value)){
            if(!in_array($yixiang_value, ['A','B','C','D'])){
                return true;
            }
            
            $yixiang_txt = db('ec_ai_task_phone_yixiang')->where('yixiang_value',$yixiang_value)->value('yixiang_txt');
            $is_yixiang = empty($yixiang_txt) ? 0 : 1;
        }
        
        $exist_info = db(getBaiYingDataTable())->where('callInstanceId',$callInstance['callInstanceId'])->find();
        if(empty($exist_info)){
            $save_data = [
                'is_yixiang'=>$is_yixiang,
                'yixiang_txt'=>$yixiang_txt,
                'yixiang_value'=>$yixiang_value,
                'callInstanceId'=>$callInstance['callInstanceId'],
                'callJobId'=>$callInstance['callJobId'],
                'customerTelephone'=> empty($callInstance['customerTelephone'])?'':$callInstance['customerTelephone'],
                'customerName'=>empty($callInstance['customerName'])?'':$callInstance['customerName'],
                'startTime'=>empty($callInstance['startTime'])?'':strtotime($callInstance['startTime']),
                'endTime'=>empty($callInstance['endTime'])?'':strtotime($callInstance['endTime']),
                'duration'=>empty($callInstance['duration'])?'':$callInstance['duration'],
                'luyinOssUrl'=>empty($callInstance['luyinOssUrl'])?'':$callInstance['luyinOssUrl'],
                'config'=> json_encode($param,JSON_UNESCAPED_UNICODE),
                'createtime'=>time(),

            ];

            $insert_id = db(getBaiYingDataTable())->insertGetId($save_data);

            return empty($insert_id) ? false : true;
        }else{
            $save_data = [
                'is_yixiang'=>$is_yixiang,
                'yixiang_txt'=>$yixiang_txt,
                'yixiang_value'=>$yixiang_value,
                'config'=> json_encode($param,JSON_UNESCAPED_UNICODE),
            ];
            $udpate_status = db(getBaiYingDataTable())->where('callInstanceId',$callInstance['callInstanceId'])->update($save_data);
            return $udpate_status === false ? false : true;
        }

        
    }
    
    
    public function _deal_calljob($param){
        $data = $param['data'];
        
        $jobName = $data['jobName'];
        $companyId = $data['companyId'];
        $callJobId = $data['callJobId'];
        $callJobStatus = $data['callJobStatus'];
        
        $exist_info = db('ec_ai_task')->where('callJobId',$callJobId)->find();
        if(empty($exist_info)){
            $save_data = [
                'jobName'=>$data['jobName'],
                'callJobId'=>$data['callJobId'],
                'callJobStatus'=>$data['callJobStatus'],
                'createtime'=>time(),

            ];

            $insert_id = db('ec_ai_task')->insertGetId($save_data);
            return empty($insert_id) ? false : true;
        }else{
            $save_data = [
                'jobName'=>$data['jobName'],
                'callJobStatus'=>$data['callJobStatus'],
            ];
            $udpate_status = db('ec_ai_task')->where('callJobId',$callJobId)->update($save_data);
            return $udpate_status === false ? false : true;
        }
        
        
    }

}
